<template>
  <t-dialog :visible.sync="visible" :header="header" confirmBtn="提交" :confirmLoading="confirmLoading"
    :onConfirm="submit" :onClose="closeDialog" :closeOnOverlayClick="false" placement="center" width="60%">
    <div style="height: 60vh; width: 98%" v-if="!dataLoading">
      <t-form ref="form" :data="formData" :rules="rules" :colon="true">
        <t-row :gutter="[14, 25]">
          <t-col :span="3">
            <t-form-item label="人才" name="talentsId">
              <t-select clearable :options="options" v-model="formData.talentsId"></t-select>
            </t-form-item>
          </t-col>
          <t-col :span="4">
            <t-form-item label="时间" name="educationDate">
              <t-date-picker clearable v-model="formData.educationDate"></t-date-picker>
            </t-form-item>
          </t-col>
          <t-col :span="5">
            <t-form-item label="地点" name="address">
              <t-input clearable v-model="formData.address"></t-input>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="内容" name="content">
              <t-textarea :autosize="{ minRows: 15 }" v-model="formData.content"></t-textarea>
            </t-form-item>
          </t-col>
          <t-col :span="12">
            <t-form-item label="相关附件" name="kjrcrchygx">
              <file-upload v-model="formData.sysAttMainVo.sysAttMainIds" moduleKey="kjrcrchygx" :fileList="fileLists"
                :multiple="true">
                <t-button variant="outline">
                  <t-icon name="cloud-upload" slot="icon" />
                  点击上传
                </t-button>
              </file-upload>
            </t-form-item>
          </t-col>
        </t-row>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue"
import { getFiles } from "@/api/common";

export default {
  name: "unit-add-edit",
  dicts: ['kpzh_manage_org_code', 'kpzh_national_economy_code', 'kpzh_unit_level', 'kpzh_source_funds'],
  components: {
    FileUpload
  },
  props: {
    objId: {
      type: [String, Number],
      default: '',
    },
    header: {
      type: String,
      default: '新增'
    },
    row: {
      type: Object,
      default: () => ({})
    },
    isUpdate: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      visible: true,
      options: [],
      formData: {
        talentsId: '',
        educationDate: '',
        address: '',
        content: '',
        sysAttMainVo: {
          sysAttMainIds: []
        }
      },
      fileIds: [],
      fileLists: [],
      dataLoading: false,
      locationOptions: [],
      menuOptions: [],
      confirmLoading: false,
      rules: {
        talentsId: [
          { required: true, message: "人才信息不能为空" }
        ],
        educationDate: [
          { required: true, message: "时间不能为空" }
        ],
        address: [
          { required: true, message: "地点不能为空" }
        ],
        content: [
          { required: true, message: "内容不能为空" }
        ],
      },
      queryParams: {
        pageNum: 1,
        pageSize: 100000,
        name: "",
        status: "",
        dateRange: []
      },
      treeProps: {
        keys: {
          value: 'id'
        },
      }
    }
  },
  created () {
    if (this.isUpdate) {
      this.getObj();
    }
    Object.assign(this.formData, this.row)
    // this.getTreeSelect();
    // this.getLocation();
    this.getTalents();
  },
  mounted () {

    this.$nextTick(() => {
      this.$refs.form.reset()
    })
  },
  methods: {
    getObj () {
      this.$api.kjrc.talentsEvents.getDetailData(this.objId).then(response => {
        response.data.sysAttMainVo = response.data.sysAttMainVo || { sysAttMainIds: [] }
        Object.assign(this.formData, response.data)
        getFiles(this.objId).then((res) => {
          this.fileLists = []
          const ids = [];
          if (res.data && res.data.length) {
            res.data.forEach((item) => {
              ids.push(item.id);
              this.fileLists.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
            })
            this.formData.sysAttMainVo.sysAttMainIds = ids;
          }
        });
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog () {
      this.$emit("update:visible", false);
    },
    submit () {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {

          this.confirmLoading = true

          if (this.isUpdate) {
            this.$api.kjrc.talentsEvents.update({
              ...this.formData,
              // sysAttMainVo: { sysAttMainIds: this.formData.fileIds }
            }).then(res => {
              this.confirmLoading = false
              this.visible = false
              this.$emit("update:visible", false);
              this.$emit("reload")
            }).catch((e) => {
              this.$message.error(e.toString());
              this.confirmLoading = false
            });
          } else {
            this.$api.kjrc.talentsEvents.add({
              ...this.formData,
              // sysAttMainVo: { sysAttMainIds: this.formData.fileIds }
            }).then(res => {
              this.confirmLoading = false
              this.visible = false
              this.$emit("update:visible", false);
              this.$emit("reload")
            }).catch((e) => {
              this.$message.error(e.toString());
              this.confirmLoading = false
            });
          }

        }
      })
    },
    getTreeSelect () {
      this.$api.kpzh.unit.treeSelect().then(response => {
        this.menuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getLocation () {
      return this.$api.system.location.treeselect().then(response => {
        this.locationOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    getTalents () {
      this.dataLoading = true;
      this.$api.kjrc.talents.myList(this.queryParams).then(response => {
        response.rows.forEach(item => {
          this.options.push({ key: item.id, label: item.name, value: item.id })
        })
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
  },
}

</script>

<style scoped></style>
